/*
 * Created by SharpDevelop.
 * User: Imgen
 * Date: 4/10/2009
 * Time: 11:55 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Drawing.Printing;
using System.Collections;
using System.IO;
using System.ComponentModel;

using Word = Microsoft.Office.Interop.Word;
using Excel = Microsoft.Office.Interop.Excel;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;


namespace iPdf
{
    #region Utility classes

    public class PageSetup
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
        public float PageWidth;
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
        public float PageHeight;

        protected void ApplyLandscape()
        {
            float temp = PageHeight;
            PageHeight = PageWidth;
            PageWidth = temp;
        }

        public PageSetup(float width, float height)
        {
            PageWidth = width;
            PageHeight = height;
        }

        public PageSetup(Word.PageSetup wordPageSetup)
            : this( wordPageSetup.PageWidth, wordPageSetup.PageHeight)
        {
        }

        public PageSetup(Excel.PageSetup excelPageSetup)
        {
            PaperSize papersize = PdfCreator.SelectPapersizeByKind((int)excelPageSetup.PaperSize);
            PageWidth = UnitConverter.HundredthInchesToPoints( papersize.Width );
            PageHeight =  UnitConverter.HundredthInchesToPoints( papersize.Height );

            if (excelPageSetup.Orientation == Microsoft.Office.Interop.Excel.XlPageOrientation.xlLandscape)
            {
                ApplyLandscape();
            }
             
        }

        public PageSetup(PowerPoint.PageSetup powerPointPageSetup)
        {
            PageWidth = powerPointPageSetup.SlideWidth;
            PageHeight = powerPointPageSetup.SlideHeight;


        }
    }

    class TataFont
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
        public float Size;
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
        public float Scaling;
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
        public float Spacing;

        public TataFont(float size, float scaling, float spacing)
        {
            Size = size;
            Scaling = scaling;
            Spacing = spacing;
        }

        public TataFont(Word.Font font)
            : this(font.Size, font.Scaling / 100.0f, font.Spacing)
        {
        }

        public TataFont(Excel.Font font)
            : this((float)font.Size, 1.0f, 0.0f)
        {
        }
    }

    public static class CoordinateConverter
    {
        private const float HorizontalOffset = 0f;
        private const float VerticalOffset = 0f;
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1045:DoNotPassTypesByReference", MessageId = "0#")]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1045:DoNotPassTypesByReference", MessageId = "0#")]
        public static void ConvertVerticalCoordinate(ref float y, PageSetup ps)
        {
            ConvertVerticalCoordinate(ref y, ps, 0.0f);
        }

        public static void ConvertVerticalCoordinate(ref float y, PageSetup ps, 
            float customOffset)
        {
            if (y < 0)
            {
                y = 0;
            }
            if (y > ps.PageHeight)
            {
                y = ps.PageHeight;
            }

            y = ps.PageHeight - y + VerticalOffset + customOffset;
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1045:DoNotPassTypesByReference", MessageId = "0#")]
        public static void ConvertHorizontalCoordinate(ref float x, PageSetup ps)
        {
            if (x < 0)
            {
                x = 0;
            }
            if (x > ps.PageWidth)
            {
                x = ps.PageWidth;
            }

            x += HorizontalOffset;
        }

        public static float ConvertVerticalCoordinate(float y, PageSetup ps)
        {
            if (y < 0)
            {
                y = 0;
            }
            if (y > ps.PageHeight)
            {
                y = ps.PageHeight;
            }

            return ps.PageHeight - y + VerticalOffset;
        }

        public static float ConvertHorizontalCoordinate(float x, PageSetup ps)
        {
            if (x < 0)
            {
                x = 0;
            }
            if (x > ps.PageWidth)
            {
                x = ps.PageWidth;
            }

            return x + HorizontalOffset;
        }
    }
    
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1815:OverrideEqualsAndOperatorEqualsOnValueTypes")]
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1815:OverrideEqualsAndOperatorEqualsOnValueTypes")]
    public struct AbsolutePosition
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
        public float X, Y;
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
        public int PageNo;
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2211:NonConstantFieldsShouldNotBeVisible")]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2211:NonConstantFieldsShouldNotBeVisible")]
        public static float PageWidth, PageHeight;
        public AbsolutePosition(float x, float y, int pageNo)
        {
            X = x;
            Y = y;
            PageNo = pageNo;
        }

        public void Adjust(PageSetup pageSetup)
        {
            CoordinateConverter.ConvertVerticalCoordinate(ref Y, pageSetup);
        }

        public void Adjust(PageSetup pageSetup, float customOffset)
        {
            CoordinateConverter.ConvertVerticalCoordinate(ref Y, pageSetup, customOffset);
        }
    }

    public static class UnitConverter
    {
        public static int PointsToHundredthInches(float points)
        {
            float pointToHundredthInchRatio = 100.0f / 72.0f;
            return (int)Math.Round(points * pointToHundredthInchRatio);
        }

        public static int HundredthInchesToPoints(float hundredthInches)
        {
            float hundredthInchToPointRatio = 72.0f / 100.0f;
            return (int)Math.Round(hundredthInches * hundredthInchToPointRatio);
        }

        public static float PointsToInches(float points)
        {
            float pointToInchRatio = 1.0f / 72.0f;
            return (float)Math.Round( points * pointToInchRatio * 100.0f) / 100.0f;
        }

        public static float PointsToTwips(float points)
        {
            float pointToTwipsRatio = 20.0f;
            return points * pointToTwipsRatio;
        }
        
        public static float PointsToMms(float points)
        {
            float pointToMmRatio = 25.4f / 72.0f;
            return points * pointToMmRatio;
        }

        public static float PointsToPixels(float point)
        {
            return 0;
        }

        public static float InchToPoints(float inch)
        {
            return 72 * inch;
        }
    }

    public class FileNameEditor : SourceGrid.Cells.Editors.TextBox
    {
        public FileNameEditor()
            : base(typeof(string) )
        {
            Control.Validating += new System.ComponentModel.CancelEventHandler(Control_Validating);
        }

        private void Control_Validating(object sender, CancelEventArgs e)
        {
            DevAge.Windows.Forms.DevAgeTextBox textBox =
                    (DevAge.Windows.Forms.DevAgeTextBox)sender;
            if (!Helper.IsValidFileName(textBox.Text))
            {
                e.Cancel = true;
            }
        }
    }

    class PasswordString
    {
        public static readonly PasswordString Empty = new PasswordString();

        private string text = string.Empty;
        public string Text
        {
            get
            {
                return text;
            }
            set
            {
                text = value;
            }
        }

        private char passwordChar = '\0';
        public char PasswordChar
        {
            get
            {
                return passwordChar;
            }
            set
            {
                passwordChar = value;
            }
        }

        public PasswordString()
        {
        }

        public PasswordString(string password, char passwordChar)
        {
            this.text = password;
            this.passwordChar = passwordChar;
        }

        public override string ToString()
        {
            if (string.IsNullOrEmpty(text))
            {
                return string.Empty;
            }
            else if (passwordChar == 0)
            {
                return text;
            }
            else
            {
                return new string(passwordChar, text.Length);
            }
        }
    }

    #endregion
}
